package field;
import geom.Segment;
import geom.Vector;

import java.util.ArrayList;



public class Bin {
	Vector lower;
	Vector upper;
	ArrayList<Segment> contained;
	Vector arrayPos;
	public Bin(Vector start, Vector size, Vector arrayPos){
		lower = start;
		upper = start.add(size);
		contained = new ArrayList<Segment>();
		this.arrayPos = arrayPos;
	}
	
	public boolean inside(Vector v){
		return v.x > lower.x && v.y > lower.y && v.z > lower.z &&
			   v.x <= upper.x && v.y <= upper.y && v.z <= upper.z;
	}
	public ArrayList<Segment> getContained(){
		return contained;
	}
	
	public void add(Segment v){
		contained.add(v);
	}
	
	public float fit(Vector v){
		float fx = Math.abs(Math.abs(v.x - lower.x) - Math.abs(upper.x - v.x));
		float fy = Math.abs(Math.abs(v.y - lower.y) - Math.abs(upper.y - v.y));
		return Math.max(1 - fx / width(), 1 - fy / height());
	}
	
	public float width(){
		return upper.x - lower.x;
	}
	public float height(){
		return upper.y - lower.y;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((lower == null) ? 0 : lower.hashCode());
		result = prime * result + ((upper == null) ? 0 : upper.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Bin other = (Bin) obj;
		if (lower == null) {
			if (other.lower != null)
				return false;
		} else if (!lower.equalTo(other.lower))
			return false;
		if (upper == null) {
			if (other.upper != null)
				return false;
		} else if (!upper.equalTo(other.upper))
			return false;
		return true;
	}
}
